﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
    public class outLinksDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int linkId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from outLinks where linkId=@linkId  ");
            htSyn.Add("linkId", linkId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得友情链接列表
        /// <summary>
        /// 获得友情链接列表
        /// </summary>
        /// <returns></returns>
        public IList<outLinks> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select linkId,linkUrl,linkTitle,Mark from outLinks");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据友情链接号查询实体
        /// <summary>
        /// 根据友情链接号查询实体
        /// </summary>
        /// <param name="linkId"></param>
        /// <returns></returns>
        public outLinks GetModel(int linkId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select linkId,linkUrl,linkTitle,Mark from outLinks ");
            strSql.Append(" where linkId=@linkId ");
            htSyn.Add("@linkId", linkId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            outLinks outLinks = new outLinks();
            if (dt.Rows.Count>0)
            {
                outLinks.linkId = Convert.ToInt32(dt.Rows[0]["linkId"]);
                outLinks.linkTitle = dt.Rows[0]["linkTitle"].ToString();
                outLinks.linkUrl = dt.Rows[0]["linkUrl"].ToString();
                outLinks.Mark = dt.Rows[0]["Mark"].ToString();
            }           
            return outLinks;
        }
        #endregion

        #region 修改友情链接信息
        /// <summary>
        /// 修改友情链接信息
        /// </summary>
        /// <param name="outLinks"></param>
        /// <returns></returns>
        public bool Update(outLinks outLinks)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update outLinks set ");
            strSql.Append("linkUrl=@linkUrl,");
            strSql.Append("linkTitle=@linkTitle,");
            strSql.Append("Mark=@Mark");
            strSql.Append(" where linkId=@linkId ");
            htSyn.Add("@linkUrl", outLinks.linkUrl);
            htSyn.Add("@linkTitle", outLinks.linkTitle);
            htSyn.Add("@Mark", outLinks.Mark);
            htSyn.Add("@linkId", outLinks.linkId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除友情链接根据友情链接号
        /// <summary>
        /// 删除友情链接根据友情链接号
        /// </summary>
        /// <param name="linkId"></param>
        /// <returns></returns>
        public bool Delete(int linkId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from outLinks ");
            strSql.Append(" where linkId=@linkId ");
            htSyn.Add("linkId", linkId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加友情链接
        /// <summary>
        /// 添加友情链接
        /// </summary>
        /// <param name="outLinks"></param>
        /// <returns></returns>
        public bool Insert(outLinks outLinks)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into outLinks([linkUrl],[linkTitle],[Mark]) values (@linkUrl,@linkTitle,@Mark)");
            htSyn.Add("@linkUrl", outLinks.linkUrl);
            htSyn.Add("@linkTitle", outLinks.linkTitle);
            htSyn.Add("@Mark", outLinks.Mark);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<outLinks> getProductsBySql(string strSql)
        {
            IList<outLinks> list = new List<outLinks>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    outLinks outLinks = new outLinks();
                    outLinks.linkId = Convert.ToInt32(dr["linkId"]);
                    outLinks.linkTitle = dr["linkTitle"].ToString();
                    outLinks.linkUrl = dr["linkUrl"].ToString();
                    outLinks.Mark = dr["Mark"].ToString();
                    list.Add(outLinks);
                }
            }
            return list;
        }
        #endregion
    }
}
